package com.zmn.mcc.admin.utils;

import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.web.multipart.MultipartFile;

import java.nio.charset.StandardCharsets;
import java.util.Map;

/**
 * 描述: oss文件上传工具类
 *
 * @author fengxuecai
 * @since 2022/8/18 14:21
 */
@Slf4j
public class ImageUploadUtil {


    /**
     * 描述: 发送multipartImage
     *
     * @param url           请求路径
     * @param file          MultipartFile流
     * @param fileParamName controller对应的接收名称
     * @param headerParams  追加的请求头信息
     * @param otherParams   其他请求参数
     * @return String       返回参数
     */
    public static String postMultipartImage(String url, MultipartFile file, String fileParamName,
                                            Map<String, String> headerParams,
                                            Map<String, String> otherParams)  {

        CloseableHttpClient httpClient = HttpClients.createDefault();
        try {

            HttpPost httpPost = new HttpPost(url);
            //添加header
            if (headerParams != null) {
                for (Map.Entry<String, String> e : headerParams.entrySet()) {
                    httpPost.addHeader(e.getKey(), e.getValue());
                }
            }
            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
            builder.setCharset(StandardCharsets.UTF_8);
            //加上此行代码解决返回中文乱码问题
            builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
            //文件流处理
            builder.addBinaryBody(fileParamName, file.getInputStream(), ContentType.MULTIPART_FORM_DATA, file.getOriginalFilename());
            if (otherParams != null) {
                for (Map.Entry<String, String> e : otherParams.entrySet()) {
                    //追加其他请求参数信息
                    builder.addTextBody(e.getKey(), e.getValue());
                }
            }
            HttpEntity entity = builder.build();
            httpPost.setEntity(entity);
            //执行提交
            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity responseEntity = response.getEntity();
            org.apache.http.Header[] headers = response.getAllHeaders();
            String body = EntityUtils.toString(responseEntity);
            Map<String, String> headerMap = Maps.newHashMap();
            if (ArrayUtils.isNotEmpty(headers)) {
                for (org.apache.http.Header header : headers) {
                    headerMap.put(header.getName(), header.getValue());
                }
            }
            return body;

        } catch (Exception e) {
            log.error("postMultipartFile error，url:{},ex:{}", url, e.getMessage());
        }
        return null;
    }
}
